package cn.zebra.dev.shop.module.product.controller;

import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.product.module.category.entity.CategoryEntity;
import cn.zebra.dev.product.module.category.entity.ProductCategoryEntity;
import cn.zebra.dev.product.module.category.service.CategoryService;
import cn.zebra.dev.product.module.category.service.ProductCategoryService;
import cn.zebra.dev.product.module.product.entity.ProductEntity;
import cn.zebra.dev.product.module.product.service.ProductService;
import cn.zebra.dev.product.module.sku.entity.ProductSkuAttributeEntity;
import cn.zebra.dev.product.module.sku.entity.ProductSkuEntity;
import cn.zebra.dev.product.module.sku.service.ProductSkuAttributeService;
import cn.zebra.dev.product.module.sku.service.ProductSkuService;
import cn.zebra.dev.shop.module.product.dto.ProductDTO;
import cn.zebra.dev.shop.module.product.vo.ProductSaleVO;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "店铺商品管理")
@RequestMapping("/shop/product")
public class ShopProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductSkuAttributeService productAttributeService;

    @Autowired
    private ProductSkuService skuService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductCategoryService productCategoryService;

    @GetMapping("/list/{shopId}")
    @ApiOperation("店铺商品列表")
    public Result<PageUtils<ProductDTO>> list(
            @PathVariable Long shopId,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> params = new HashMap<>();
        params.put("shopId", shopId);
        params.put("limit", limit);
        params.put("page", page);
        PageUtils<ProductEntity> pageUtils = productService.findPageByShopId(params);
        PageUtils<ProductDTO> data = new PageUtils<>(pageUtils);
        List<ProductEntity> list = pageUtils.getList();
        if (list.size() > 0) {
            List<ProductDTO> dtoList = new LinkedList<>();
            list.forEach(entity -> {
                ProductDTO dto = new ProductDTO();
                BeanUtils.copyProperties(entity, dto);
                dtoList.add(dto);
            });
            data.setList(dtoList);
        }
        return Result.success(data);
    }

    @GetMapping("/get/{id}")
    @ApiOperation("查看商品信息")
    public Result<ProductDTO> get(@PathVariable Long id) {
        ProductEntity product = productService.findById(id);
        List<ProductSkuAttributeEntity> attrs = productAttributeService.findListByProductId(id);
        List<ProductSkuEntity> skuList = skuService.findListByProductId(id);
        ProductDTO dto = new ProductDTO();
        BeanUtils.copyProperties(product, dto);
        ProductCategoryEntity productCategoryEntity = productCategoryService.findByProductId(id);
        if (productCategoryEntity != null) {
            Long categoryId = productCategoryEntity.getCategoryId();
            dto.setCategoryId(categoryId);
            CategoryEntity categoryEntity = categoryService.findById(categoryId);
            dto.setCategoryName(categoryEntity.getCategoryName());
        }
        dto.setSkuList(skuList);
        dto.setAttrEntities(attrs);
        return Result.success(dto);
    }

    @PostMapping("/sale")
    @ApiOperation("商品上下架设置(批量)")
    public Result sale(@Valid @RequestBody ProductSaleVO params) {
//        ValidatorUtils.validateEntity(params);
        ProductEntity entity = new ProductEntity();
        entity.setSaleFlag(params.getSale());
        UpdateWrapper<ProductEntity> wrapper = new UpdateWrapper<>();
        wrapper.in("id", params.getProductId());
        if (productService.update(entity, wrapper)) {
            return Result.success("设置成功!");
        }
        return Result.error("设置失败!");
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除商品信息")
    public Result delete(@PathVariable Long id) {
        ProductEntity entity = new ProductEntity();
        entity.setDeleteFlag(true);
        if (productService.update(entity, "id = {0}", id)) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败!");
    }

}
